﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Interfaz
{
    /// <summary>
    /// Interaction logic for RegistrarEmpleado.xaml
    /// </summary>
    public partial class RegistrarEmpleado : UserControl
    {
        public RegistrarEmpleado()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            uint iValue = 0;

            if (UInt32.TryParse(textBox.Text, out iValue) == false)
            {
                TextChange textChange = e.Changes.ElementAt<TextChange>(0);
                int iAddedLength = textChange.AddedLength;
                int iOffset = textChange.Offset;

                textBox.Text = textBox.Text.Remove(iOffset, iAddedLength);
            }
        }
        public bool validarTextBoxCi()
        {
            try
            {
                Procesos.Validar.validarCI(textBox1.Text);
                return true;
            }
            catch (Exception ex)
            {
                setMensaje(textBox1, ex.Message);
                return false;
            }
        }
        private void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            validarTextBoxCi();
        }
        public void setMensaje(TextBox text, string mensaje)
        {


            text.Background = System.Windows.Media.Brushes.Red;


            text.ToolTip = mensaje;

        }
        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox text = sender as TextBox;
            text.ToolTip = null;
            text.Background = System.Windows.Media.Brushes.White;
        }
        public bool validarTextBox2()
        {
            try
            {
                Procesos.Validar.validarVacio(textBox2.Text);
                return true;
            }
            catch (Exception ex)
            {
                setMensaje(textBox2, ex.Message);
                return false;
            }
        }
        private void textBox2_LostFocus(object sender, RoutedEventArgs e)
        {
            validarTextBox2();
        }
        public bool validarTextBoxApellido()
        {
            try
            {
                Procesos.Validar.validarVacio(textBox3.Text);
                return true;
            }
            catch (Exception ex)
            {
                setMensaje(textBox3, ex.Message);
                return false;
            }
        }
        private void textBox3_LostFocus(object sender, RoutedEventArgs e)
        {
            validarTextBoxApellido();
        }

        public bool validarTextBoxTelefono()
        {
            try
            {
                Procesos.Validar.validarTelefono(textBox4.Text);
                return true;
            }
            catch (Exception ex)
            {
                setMensaje(textBox4, ex.Message);
                return false;
            }
        }
        private void textBox4_LostFocus(object sender, RoutedEventArgs e)
        {
            validarTextBoxTelefono();
        }
        public bool validarTextBoxCelular()
        {
            try
            {
                Procesos.Validar.validarCelular(textBox5.Text);
                return true;
            }
            catch (Exception ex)
            {
                setMensaje(textBox5, ex.Message);
                return false;
            }
        }
        private void textBox5_LostFocus(object sender, RoutedEventArgs e)
        {
            validarTextBoxCelular();
        }
        public bool validarTextBoxMail()
        {
            try
            {
                Procesos.Validar.validarMail(textBox6.Text);
                return true;
            }
            catch (Exception ex)
            {
                setMensaje(textBox6, ex.Message);
                return false;
            }
        }
        private void textBox6_LostFocus(object sender, RoutedEventArgs e)
        {
            validarTextBoxMail();
        }
        public bool validarTextBoxDireccion()
        {
            try
            {
                Procesos.Validar.validarVacio(textBox7.Text);
                return true;
            }
            catch (Exception ex)
            {
                setMensaje(textBox7, ex.Message);
                return false;
            }
        }
        private void textBox7_LostFocus(object sender, RoutedEventArgs e)
        {
            validarTextBoxDireccion();
        }
        public bool validarTodos()
        {
            bool t1 = validarTextBox2(), t2 = validarTextBoxApellido(), t3 = validarTextBoxCelular(), t4 = validarTextBoxCi(), t5 = validarTextBoxDireccion(), t6 = validarTextBoxMail(), t7 = validarTextBoxTelefono();
            return t1 && t2 && t3 && t4 && t5 && t6 && t7;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (validarTodos())
                {
                    Procesos.GestorRegistro.registrarPersona(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, comboBox1.SelectedIndex);
                    MessageBox.Show("Registro realizado con exito", "Completo", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            
                }
                else
                    MessageBox.Show("revise los campos marcados con rojo", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


    }
}
